package com.joe.learn.jdk8;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class LambdaDemo {

    public static void main(String[] args) {
//        runDemo();
        score();
    }

    public static void runDemo() {

        //传统定义
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread());
            }
        };
        runnable1.run();

        //Lambda 形式
        Runnable runnable2 = () -> System.out.println(Thread.currentThread());
        runnable2.run();
    }

    public static void score() {

        List<Student> listJdk7 = new ArrayList<>(
                Arrays.asList(
                        new Student(1, "张三", 41),
                        new Student(2, "王二", 92),
                        new Student(3, "李四", 52),
                        new Student(4, "王五", 81),
                        new Student(5, "赵六", 67)
                )
        );

        Iterator<Student> listIt = listJdk7.iterator();
        while (listIt.hasNext()) {
            Student next = listIt.next();
            if (next.getScore().intValue() < 80) {
                listIt.remove();
            }
        }
        System.out.println(JSON.toJSONString(listJdk7, SerializerFeature.PrettyFormat));

        List<Student> listJdk8 = new ArrayList<>(
                Arrays.asList(
                        new Student(1, "张三", 41),
                        new Student(2, "王二", 92),
                        new Student(3, "李四", 52),
                        new Student(4, "王五", 81),
                        new Student(5, "赵六", 67)
                )
        );
        List<Student> collect = listJdk8.stream().filter(x -> x.getScore() > 80).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(collect, SerializerFeature.PrettyFormat));

    }

    @Data
    static class Student {
        private String name;
        private Integer id;
        private Integer score;
        private String level;

        public Student(Integer id, String name, Integer score) {
            this.name = name;
            this.id = id;
            this.score = score;
        }
    }

}
